package com.budwk.app.util.JsonToEntity;

/**
 * @desc: 方法描述
 * @Author: laoschiang
 * @Date: 2023/11/30 15:19
 * @info:
 */
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

public class VuePageGenerator {

    public static String generateVuePage(String json) {
        StringBuilder vuePage = new StringBuilder();
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            JsonNode rootNode = objectMapper.readTree(json);
            vuePage.append("<template>\n");
            vuePage.append("  <div>\n");

            generateVueElements(rootNode, vuePage);

            vuePage.append("  </div>\n");
            vuePage.append("</template>\n");

        } catch (IOException e) {
            e.printStackTrace();
        }

        return vuePage.toString();
    }

    private static void generateVueElements(JsonNode node, StringBuilder vuePage) {
        Iterator<Map.Entry<String, JsonNode>> fields = node.fields();

        while (fields.hasNext()) {
            Map.Entry<String, JsonNode> entry = fields.next();
            String key = entry.getKey();
            Object getvalue = entry.getValue();
            JsonNode value = entry.getValue();

            // Assuming all fields are strings for simplicity
            String elementType = determineElementType(value);

            vuePage.append("    <div class=\"form-group\">\n");
            vuePage.append("      <label for=\"" + key + "\">" + getvalue + ":</label>\n");

            if ("input".equals(elementType)) {
                vuePage.append("      <input type=\"text\" v-model=\"" + key + "\" />\n");
            } else if ("radio".equals(elementType)) {
                vuePage.append("      <input type=\"radio\" v-model=\"" + key + "\" value=\"option1\" /> Option 1\n");
                vuePage.append("      <input type=\"radio\" v-model=\"" + key + "\" value=\"option2\" /> Option 2\n");
                // Add more options as needed
            }

            vuePage.append("    </div>\n");
        }
    }

    private static String determineElementType(JsonNode node) {
        // Simplified logic, you might need to enhance it based on your actual data
        if (node.isTextual()) {
            return "input";
        } else if (node.isArray()) {
            return "radio";
        } else {
            // Handle other types as needed
            return "input";
        }
    }

}
